package hellfire.system.sound;

import hellfire.system.engine.Main;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.SparseIntArray;


public class Sound 
{
	//private static Vector<MediaPlayer> oneTimeSounds = new Vector<MediaPlayer>();
	private static SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
	private static boolean isSound = true;
	private static SparseIntArray soundDb = new SparseIntArray();
	
	static
	{
		//int handle = soundPool.load(Main.currentActivity, R.raw.thebigone, 1);
	}
	
	public static boolean isSound()
	{
		return isSound;
	}
	
	public static void disableSound()
	{
		isSound = false;
	}
	
	public static void enableSound()
	{
		isSound = true;
	}
	
	public static MediaPlayer playSound(final int resource)
	{
		int handle = soundDb.get(resource, -1);
		if(handle == -1)
		{
			handle = soundPool.load(Main.Activity, resource, 1);
			soundDb.append(resource, handle);
			soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() 
			{
				public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
				{
					playSound(resource);
				}
			});
			return null;
		}
		float volume = isSound ? 1 : 0;
		soundPool.play(handle, volume, volume, 1, 0, 1);
	
		return null;
	}
	
	public static MediaPlayer playSound(int resource, OnCompletionListener listener)
	{
		MediaPlayer mp = MediaPlayer.create(Main.Activity, resource);
        if(mp == null) return null;
		mp.setOnCompletionListener(listener);
        if(!isSound) mp.setVolume(0, 0);
        mp.start();
        return mp;
	}
	
	

}
